package com.jy.modules.cims.component.funds.rest;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jy.modules.cims.common.open.api.enums.EntryResponseEnum;
import com.jy.modules.cims.common.sysUtils.ExceptionUtils;
import com.jy.modules.cims.common.sysUtils.SimpleBeanUtils;
import com.jy.modules.cims.component.api.IJieYueSend;
import com.jy.modules.cims.component.common.service.TCMmFundsBorrowerBankactService;
import com.jy.modules.cims.data.common.dto.TCMmFundsBorrowerBankactDTO;
import com.jy.modules.cims.data.interact.api.JieYueResponseData;
import com.jy.modules.cims.data.interact.api.request.funds.RequestAccountDrawSearchDTO;
import com.jy.modules.cims.data.interact.api.request.funds.RequestBankInfoCheckSearchDTO;
import com.jy.modules.cims.data.interact.api.request.funds.RequestBaseFundsBankActDTO;
import com.jy.modules.cims.data.interact.api.request.funds.RequestFundsAccountDrawDTO;
import com.jy.modules.cims.data.interact.api.request.funds.RequestFundsCardupdateSearchDTO;
import com.jy.modules.cims.data.interact.api.request.funds.RequestMessageFundsSendDTO;
import com.jy.modules.cims.data.interact.api.response.funds.ResponseAccountDrawSearchDTO;
import com.jy.modules.cims.data.interact.api.response.funds.ResponseBankInfoCheckSearchDTO;
import com.jy.modules.cims.data.interact.api.response.funds.ResponseBaseFundsBankActDTO;
import com.jy.modules.cims.data.interact.api.response.funds.ResponseFundsAccountDrawDTO;
import com.jy.modules.cims.data.interact.api.response.funds.ResponseFundsCardupdateSearchDTO;
import com.jy.modules.cims.data.interact.api.response.funds.ResponseMessageFundsSendDTO;
import com.jy.modules.platform.sysconfig.service.SysConfigService;
import com.jy.platform.core.message.ResponseMsg;


/**
 *资金存管帐号基本查询
 * @author lihong
 * @date
 */
@Controller
@RequestMapping(value = "/api/cims/fundsBankInfoRest")
public class FundsBankInfoRest {

	private static Logger logger = LoggerFactory.getLogger(FundsBankInfoRest.class);
	@Autowired
	@Qualifier("jieYueSendService")
	private IJieYueSend jieYueSend;
	
	@Autowired
	@Qualifier("com.jy.modules.platform.sysconfig.service.SysConfigService")
	private SysConfigService sysConfigService;
	
	@Autowired
    @Qualifier("com.jy.modules.cims.component.common.service.TCMmFundsBorrowerBankactService")
    private TCMmFundsBorrowerBankactService fundsBorrowerBankactService;
	
	private String cooperorgCode = "";
	/**
	 * 银行账户查询
	 * @author lihong
	 * @date  2017年4月24日
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/searchFundsAccount", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg<ResponseBaseFundsBankActDTO>> searchFundsAccount(@RequestBody RequestBaseFundsBankActDTO obj) throws Exception{
		ResponseMsg<ResponseBaseFundsBankActDTO> responseMsg = new ResponseMsg<ResponseBaseFundsBankActDTO>();
		String cooperorgCode=obj.getCooperorgCode();
		try {
			ResponseBaseFundsBankActDTO responseContent = new ResponseBaseFundsBankActDTO();
			if("XS".equals(cooperorgCode)){
				//把贷前的数据 复制到  向上接口DTO
				RequestBankInfoCheckSearchDTO requestContent = new RequestBankInfoCheckSearchDTO();
				SimpleBeanUtils.copyBean2Bean(requestContent, obj);
				//发送向上
				JieYueResponseData jyresponseDate= jieYueSend.searchFundsAccountCheck(cooperorgCode, requestContent);
				if(jyresponseDate!=null){
					if(EntryResponseEnum.SUCESS21001.getCode().equals(jyresponseDate.getRetCode())){
						ResponseBankInfoCheckSearchDTO responseData = (ResponseBankInfoCheckSearchDTO)jyresponseDate.getData(); 
						//用户注册
						TCMmFundsBorrowerBankactDTO fundsBorrowerBankactDTO = new TCMmFundsBorrowerBankactDTO();
						fundsBorrowerBankactDTO.setCooperorgcode(cooperorgCode);//机构编号
						fundsBorrowerBankactDTO.setIdnumber(requestContent.getIdnumber());
						fundsBorrowerBankactDTO.setIdtype(requestContent.getIdtype());
						fundsBorrowerBankactDTO.setUsername(requestContent.getUsername());
						fundsBorrowerBankactDTO.setUserphonenum(requestContent.getUserphonenum());
						fundsBorrowerBankactDTO.setCreateTime(new Date());
						fundsBorrowerBankactDTO.setUserid(responseData.getUserId());
						fundsBorrowerBankactService.insertTCMmFundsBorrowerBankact(fundsBorrowerBankactDTO);
						
						responseContent = ResponseBaseFundsBankActDTO.isSuccess();
						responseContent.setId(requestContent.getIdnumber());
						responseContent.setMessage("账户开通成功！");
					}else{
						logger.info("合作机构【{}】，银行账账户开户查询接口异常编码【{}】，异常描述【{}】！",cooperorgCode,jyresponseDate.getRetCode(),jyresponseDate.getMessage());
						responseContent = ResponseBaseFundsBankActDTO.isFail();
						responseContent.setMessage("电子账户开户查询接口异常");
					}
				}else{
					logger.info("合作机构【{}】，银行账账户开户查询接口，返回数据为空！",cooperorgCode);
					responseContent = ResponseBaseFundsBankActDTO.isFail();
					responseContent.setMessage("电子账户开户查询接口返回数据为空");
				}
			}else{
				responseContent = ResponseBaseFundsBankActDTO.isSuccess();
				responseContent.setMessage("无需开通电子账户");
			}
			//调用银行账账户开户查询
			responseMsg.setResponseBody(responseContent);
		
		} catch (Exception e) {
			logger.error("合作机构【{}】，报错{}！",cooperorgCode,ExceptionUtils.getStackTraceAsString(e));
		}
		ResponseEntity<ResponseMsg<ResponseBaseFundsBankActDTO>> responseEntity=new ResponseEntity<ResponseMsg<ResponseBaseFundsBankActDTO>>(responseMsg, HttpStatus.OK);
		return responseEntity;
	}
	
	/**
	 * 银行账户变更
	 * @author lihong
	 * @date  2017年4月24日
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/searchFundsCardupdate", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg<ResponseFundsCardupdateSearchDTO>> searchFundsCardupdate(@RequestBody RequestFundsCardupdateSearchDTO obj) throws Exception{
		ResponseMsg<ResponseFundsCardupdateSearchDTO> responseMsg = new ResponseMsg<ResponseFundsCardupdateSearchDTO>();
		cooperorgCode=sysConfigService.queryValueByCode("FUNDS_BALANCE_COOPERORG");
		try {
			//调用银行账户变更接口
			JieYueResponseData jyresponseDate= jieYueSend.searchFundsCardupdate(cooperorgCode, obj);
			if(jyresponseDate!=null){
				if(EntryResponseEnum.SUCESS21001.getCode().equals(jyresponseDate.getRetCode())){
					responseMsg.setResponseBody((ResponseFundsCardupdateSearchDTO)jyresponseDate.getData());
				}else{
					logger.info("合作机构【{}】，银行账户变更接口异常编码【{}】，异常描述【{}】！",cooperorgCode,jyresponseDate.getRetCode(),jyresponseDate.getMessage());
				}
			}else{
				logger.info("合作机构【{}】，银行账户变更接口，返回数据为空！",cooperorgCode);
			}
		} catch (Exception e) {
			logger.error("合作机构【{}】，报错{}！",cooperorgCode,ExceptionUtils.getStackTraceAsString(e));
		}
		ResponseEntity<ResponseMsg<ResponseFundsCardupdateSearchDTO>> responseEntity=new ResponseEntity<ResponseMsg<ResponseFundsCardupdateSearchDTO>>(responseMsg, HttpStatus.OK);
		return responseEntity;
	}
	
	/**
	 * 账户提现
	 * @author lihong
	 * @date  2017年4月24日
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/searchFundsAccountDraw", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg<ResponseFundsAccountDrawDTO>> searchFundsAccountDraw(@RequestBody RequestFundsAccountDrawDTO obj) throws Exception{
		ResponseMsg<ResponseFundsAccountDrawDTO> responseMsg = new ResponseMsg<ResponseFundsAccountDrawDTO>();
		cooperorgCode=sysConfigService.queryValueByCode("FUNDS_BALANCE_COOPERORG");
		try {
			//调用账户提现接口
			JieYueResponseData jyresponseDate= jieYueSend.searchFundsAccountDraw(cooperorgCode, obj);
			if(jyresponseDate!=null){
				if(EntryResponseEnum.SUCESS21001.getCode().equals(jyresponseDate.getRetCode())){
					responseMsg.setResponseBody((ResponseFundsAccountDrawDTO)jyresponseDate.getData());
				}else{
					logger.info("合作机构【{}】，账户提现接口异常编码【{}】，异常描述【{}】！",cooperorgCode,jyresponseDate.getRetCode(),jyresponseDate.getMessage());
				}
			}else{
				logger.info("合作机构【{}】，账户提现接口，返回数据为空！",cooperorgCode);
			}
		} catch (Exception e) {
			logger.error("合作机构【{}】，报错{}！",cooperorgCode,ExceptionUtils.getStackTraceAsString(e));
		}
		
		ResponseEntity<ResponseMsg<ResponseFundsAccountDrawDTO>> responseEntity=new ResponseEntity<ResponseMsg<ResponseFundsAccountDrawDTO>>(responseMsg, HttpStatus.OK);
		return responseEntity;
	}
	/**
	 * 提现结果查询
	 * @author lihong
	 * @date  2017年4月24日
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/searchFundsAccountDrawSearch", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg<ResponseAccountDrawSearchDTO>> searchFundsAccountDrawSearch(@RequestBody RequestAccountDrawSearchDTO obj) throws Exception{
		ResponseMsg<ResponseAccountDrawSearchDTO> responseMsg = new ResponseMsg<ResponseAccountDrawSearchDTO>();
		cooperorgCode=sysConfigService.queryValueByCode("FUNDS_BALANCE_COOPERORG");
		try {
			//调用账户提现接口
			JieYueResponseData jyresponseDate= jieYueSend.searchFundsAccountDrawSearch(cooperorgCode, obj);
			if(jyresponseDate!=null){
				if(EntryResponseEnum.SUCESS21001.getCode().equals(jyresponseDate.getRetCode())){
					responseMsg.setResponseBody((ResponseAccountDrawSearchDTO)jyresponseDate.getData());
				}else{
					logger.info("合作机构【{}】，提现结果查询接口异常编码【{}】，异常描述【{}】！",cooperorgCode,jyresponseDate.getRetCode(),jyresponseDate.getMessage());
				}
			}else{
				logger.info("合作机构【{}】，提现结果查询接口，返回数据为空！",cooperorgCode);
			}
		} catch (Exception e) {
			logger.error("合作机构【{}】，报错{}！",cooperorgCode,ExceptionUtils.getStackTraceAsString(e));
		}
		
		ResponseEntity<ResponseMsg<ResponseAccountDrawSearchDTO>> responseEntity=new ResponseEntity<ResponseMsg<ResponseAccountDrawSearchDTO>>(responseMsg, HttpStatus.OK);
		return responseEntity;
	}
	/**
	 * 发送短信验证码
	 * @author lihong
	 * @date  2017年4月24日
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/messageFundsSend", method = RequestMethod.POST)
	public ResponseEntity<ResponseMsg<ResponseMessageFundsSendDTO>> messageFundsSend(@RequestBody RequestMessageFundsSendDTO obj) throws Exception{
		ResponseMsg<ResponseMessageFundsSendDTO> responseMsg = new ResponseMsg<ResponseMessageFundsSendDTO>();
		cooperorgCode=sysConfigService.queryValueByCode("FUNDS_BALANCE_COOPERORG");
		try {
			//调用发送短信验证码接口
			JieYueResponseData jyresponseDate= jieYueSend.messageFundsSend(cooperorgCode, obj);
			if(jyresponseDate!=null){
				if(EntryResponseEnum.SUCESS21001.getCode().equals(jyresponseDate.getRetCode())){
					responseMsg.setResponseBody((ResponseMessageFundsSendDTO)jyresponseDate.getData());
				}else{
					logger.info("合作机构【{}】，发送短信验证码接口异常编码【{}】，异常描述【{}】！",cooperorgCode,jyresponseDate.getRetCode(),jyresponseDate.getMessage());
				}
			}else{
				logger.info("合作机构【{}】，发送短信验证码接口，返回数据为空！",cooperorgCode);
			}
		} catch (Exception e) {
			logger.error("合作机构【{}】，报错{}！",cooperorgCode,ExceptionUtils.getStackTraceAsString(e));
		}
		
		ResponseEntity<ResponseMsg<ResponseMessageFundsSendDTO>> responseEntity=new ResponseEntity<ResponseMsg<ResponseMessageFundsSendDTO>>(responseMsg, HttpStatus.OK);
		return responseEntity;
	}
}
